const util = {
    // 获取原型链
    prototype(node) {
        let prototypes = [];

        if (!node.__proto__) return [];

        prototypes.push(node.__proto__, ...this.prototype(node.__proto__));
        return prototypes;
    },
    //input node 判断
    isInputForm(node) {
        return node instanceof HTMLInputElement;
    },
    // 添加原型方法
    substr(start, num) {
        return this[util.isInputForm(this) ? 'value' : 'textContent'].substr(
            start,
            num
        );
    },
    //获取节点树
    tree(node) {
        return Array.from(node.childNodes)
            .filter((node) => node.nodeType == 1)
            .map((node) => ({ tag: node.nodeName, children: this.tree(node) }));
    },
    //获取上面的同辈节点
    previousSibling(element) {
        let nodes = [];
        while ((element = element.previousSibling)) {
            if (element.nodeType == 1) nodes.push(element);
        }

        return nodes;
    },
    nextSibling(element) {
        let nodes = [];
        while ((element = element.nextSibling)) {
            if (element.nodeType == 1) nodes.push(element);
        }

        return nodes;
    },
    //所有同辈节点
    siblings(node) {
        // return Array.from(node.parentNode.childNodes).filter(n => n.nodeType == 1)
        return [...this.previousSibling(node),...this.nextSibling(node)];
    },
    //用标签名获取同辈节点  
    siblingsByTagName(node, tagName) {
        return this.siblings(node).filter((node) => {
            return node.tagName == tagName.toUpperCase();
        });
    },
    //删除元素,支持单个元素与元素集合
    removeChild(element, node) {
        const nodes = Array.isArray(node) ? node : [node];
        return nodes.forEach((n) => element.removeChild(n));
    },
    //创建元素
    createElement(tag, options) {
        const element = document.createElement(tag);
        for (const [key, value] of Object.entries(options)) {
            element[key] = value;
        }
        return element;
    },
    //----------------------作业-------------------------
    //删除子元素中包含xx内容的
    //uitl.removeHasContent(app,a,'tom')   div#app>section>main>a>"tom"
    removeChildNodeHasContent(node,childnode,content) {
        let removeList=Array.from(node.childNodes).filter((node)=> {
            if(node.tagName==childnode.toUpperCase()){
                if(node.textContent.indexOf(content)!=-1) return true;
            }
            return false;
        })
        this.removeChild(node,removeList)
    },
};

export default util;
